<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\User;
use App\Models\UserType;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use App\Models\User AS UserModel;
use App\Models\Goods;
use App\Admin\Actions\User\Restore;
use App\Admin\Actions\User\BatchRestore;
use App\Models\GoodsCollection;
class UserCollectionController extends AdminController
{
    /**
     * Index interface.
     *
     * @param Content $content
     *
     * @return Content
     */
    public function index(Content $content)
    {
        $goods_id = \Request::get("goods_id");
        $goods = Goods::find($goods_id);
        return $content
            ->title($goods->name."的收藏者列表")
            ->description($this->description()['index'] ?? trans('admin.list'))
            ->body($this->grid());
    }
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new User(['userType']), function (Grid $grid) {
            $goods_id = \Request::get("goods_id");
            $user_ids = GoodsCollection::where("goods_id",$goods_id)->pluck("user_id")->toArray();
//            dd($user_ids);
            $grid->model()->whereIn("id",$user_ids);
//            dd($grid->model());
            $grid->column('id')->sortable();
            $grid->column('nickname')->editable();
//            $grid->column('phone');
            $grid->column('l_m')->display(function ($l_m){
                switch ($l_m){
                    case 0:
                       return 'm';
                       break;
                    case 1:
                        return "l";
                        break;
                    default:
                        return "m";
                }
            });
            $grid->column('come_from')->display(function ($come_from){
                return UserModel::$comeFromMap[$come_from];
            });
            $grid->column('userType.name',"用户类别");
            $grid->column('has_wx')->display(function ($has_wx){
                switch ($has_wx){
                    case 0:
                        return "无";
                        break;
                    case 1:
                        return "有";
                        break;
                    default:
                        return "无";
                }
            });
            $grid->column('deal_time');
            $grid->column('log_time')->sortable();
            $grid->column("sin_in_day")->sortable();


            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
                $filter->like("nickname");
                $filter->like("phone");
                $filter->equal("come_from")->select(
                  UserModel::$comeFromMap
                );
                $filter->equal("cate_type")->select(
                  UserType::all()->pluck('name','id')
                  ->toArray()
                );
                // 范围过滤器，调用模型的`onlyTrashed`方法，查询出被软删除的数据。
//                $filter->scope('trashed', '回收站')->onlyTrashed();
            });
            // 禁用详情按钮
            $grid->disableViewButton();
            $grid->disableDeleteButton();
            $grid->disableCreateButton();
            $grid->disableEditButton();
            // 也可以这样
            $grid->disableBatchDelete();
            $grid->actions(function (Grid\Displayers\Actions $actions) {
                if (request('_scope_') == 'trashed') {
                    $actions->append(new Restore(UserModel::class));
                }
                $count = GoodsCollection::where("user_id",$actions->row->id)->count();
                $url = <<<URL
/cpgdeal/collection_goods?user_id={$actions->row->id}
URL;
//dd($url);
                $actions->append('<a href="'.$url.'"><i class="fa fa-heart"></i>收藏列表('.$count.')</a>');
            });
            $grid->batchActions(function (Grid\Tools\BatchActions $batch) {
                if (request('_scope_') == 'trashed') {
                    $batch->add(new BatchRestore(UserModel::class));
                }
            });
        });
    }


}
